package com.xiaodou.mjf.web.xface.core.controller.ibp.v1.threads;

import com.xiaodou.mjf.entity.core.InvokeLog;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;
import com.xiaodou.mjf.infrastructure.payeco.tools.http.HttpClient;

import java.net.URLEncoder;

/**
 * Created by Rocky on 15/4/3.
 */
public class AsyncNotifyThread extends Thread {

    private String url;
    private String orderId;
    private Double amount;
    private String channelId;
    private String tradeCode;
    private String payTime;
    private String code;
    private String message;

    public AsyncNotifyThread(String url,String orderId,Double amount,String channelId,String tradeCode,String payTime,String code,String message){
        this.url = url;
        this.orderId = orderId;
        this.amount = amount;
        this.channelId = channelId;
        this.tradeCode = tradeCode;
        this.payTime = payTime;
        this.code = code;
        this.message = message;
    }

    @Override
    public void run() {
        try {


            String param = "order_id=" + orderId + "&amount=" + amount + "&channel_id="
                    + channelId + "&trade_code=" + tradeCode + "&pay_time=" + payTime + "&code="+code+"&message="+ URLEncoder.encode(message,"UTF-8");

            url = url + "?" + param;
            System.out.println("第一次发送:" + url);

            String result = new HttpClient().getConnection(url, null, "UTF-8", "UTF-8", 5000, 5000, null);
            int i = 0;
            while (!result.toUpperCase().equals("SUCCESS") && i < 60) {
                i += 10;
                Thread.sleep(1000 * i);
                result = new HttpClient().getConnection(url, null, "UTF-8", "UTF-8", 5000, 5000, null);
                System.out.println("第" + i + "次发送:" + url);
            }
            System.out.println("received SUCCESS");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
